package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@Api(tags = "C端-地址簿接口")
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 获取当前用户的地址簿
     */
    @GetMapping("/list")
    @ApiOperation("C端获取当前用户的地址簿")
    public Result<List<AddressBook>> getAddressBookList () {
        List<AddressBook> addressBookList = addressBookService.getAddressBookList();
        return Result.success(addressBookList);
    }

    /**
     * 新增收货地址
     * @param addressBook
     * @return
     */
    @ApiOperation("C端新增地址")
    @PostMapping
    public Result addAddress (@RequestBody AddressBook addressBook) {
        log.info("C端用户{}新增地址:{}", BaseContext.getCurrentId(),addressBook);
        addressBookService.addAddress(addressBook);
        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getAddresById (@PathVariable Integer id) {
        log.info("根据id查询地址:{}",id);
        AddressBook addressBook = addressBookService.getAddresById(id);
        return Result.success(addressBook);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result setDefaultAddress (@RequestBody AddressBook addressBook) {
        log.info("C端设置默认地址:{}",addressBook.getId());
        addressBookService.setDefaultAddress(addressBook);
        return Result.success();
    }


    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefaultAddress () {
        return Result.success(addressBookService.getDefaultAddress());
    }


    /**
     * 根据id修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("根据id修改地址")
    public Result updateAddress (@RequestBody AddressBook addressBook) {
        log.info("根据id修改地址:{}",addressBook);
        addressBookService.updateAddress(addressBook);
        return Result.success();
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteAddress (Long id) {
        addressBookService.deleteAddress(id);
        return Result.success();
    }
}


